Shader "Custom/魂_多色替换"
{
    Properties
    {
        //当然就是材质啦
        [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}

        _TargetColors1 ("被替换的颜色1", Color) = (0, 0, 0, 1) 
        _ReplaceColors1 ("替换为的颜色1", Color) = (0, 0, 0, 0) 

        _EnabledTargetRanges ("是否启用目标颜色范围", Int) = 0
        _TargetRanges ("在一定范围内匹配目标颜色", Range(0, 1)) = 0.01

        _CilpUnMatch("是否删除其他颜色",Int)=0


        
    }
    SubShader
    {
        Tags {
            "Queue" = "Transparent"
            "IgnoreProjector" = "True"
            "RenderType" = "Transparent"
            "PreviewType" = "Plane"
            "CanUseSpriteAtlas" = "True"
        }

        Cull Off
        Lighting Off
        ZWrite Off
        Blend One OneMinusSrcAlpha


        Pass 
        {
            CGPROGRAM

            #pragma vertex vert
            #pragma fragment frag
            #pragma target 2.0
            //#pragma multi_compile_instancing
            //#pragma multi_compile_local _ PIXELSNAP_ON
            //#pragma multi_compile _ ETC1_EXTERNAL_ALPHA
            //#include "UnitySprites.cginc"

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
            };

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv;
                return o;
            }

            sampler2D _MainTex;

            fixed4 _TargetColor1; 
            fixed4 _ReplaceColor1;

            fixed _TargetRange;


            int _CilpUnMatch;
            int _EnabledTargetRanges;
            fixed4 TargetColors[100];
            fixed4 ReplaceColors[100];
            fixed TargetRanges[100];
            int colorSize;

            fixed4 frag (v2f i) : SV_Target
            {

                fixed4 col = tex2D(_MainTex, i.uv);
                //替换颜色
                if(col.a <= 0){
                    clip(-1);
                }else{
                    int matched=0;
                    for(int i=0;i<colorSize;i++){

                        fixed4 tc=TargetColors[i];
                        fixed4 rc=ReplaceColors[i];
                        fixed tr=0.0;
                        if(_EnabledTargetRanges!=0){
                            tr=TargetRanges[i];
                        }
                        int a=abs(-1);
                        if( abs(col.r - tc.r)<=tr && abs(col.g - tc.g)<=tr && abs(col.b - tc.b)<=tr ){
                            col = rc;
                            matched=1;
                            break;
                        }
                    }
                    if(matched==0 && _CilpUnMatch!=0){
                        clip(-1);
                    }

                }
                return col;
            }
            ENDCG
        }
    }
}

